//
//  Tools.m
//  SleepingElf
//
//  Created by karong on 2023/10/24.
//

#import "Tools.h"
#import "sys/utsname.h"
#import <CommonCrypto/CommonDigest.h>

@implementation Tools
+ (UIViewController *)rootViewController {
    return [UIApplication sharedApplication].keyWindow.rootViewController;
}

+ (UIViewController *)topViewController:(UIViewController *)vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self topViewController:[(UINavigationController *)vc topViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self topViewController:[(UITabBarController *)vc selectedViewController]];
    } else {
        return vc;
    }
    return nil;
}

+ (NSString *)phoneModel {
  // 需要#import "sys/utsname.h"
     struct utsname systemInfo;
     uname(&systemInfo);
     NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
     //iPhone
     if ([deviceString isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";
     if ([deviceString isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
     if ([deviceString isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
     if ([deviceString isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
     if ([deviceString isEqualToString:@"iPhone3,2"])    return @"Verizon iPhone 4";
     if ([deviceString isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
     if ([deviceString isEqualToString:@"iPhone5,1"])    return @"iPhone 5";
     if ([deviceString isEqualToString:@"iPhone5,2"])    return @"iPhone 5";
     if ([deviceString isEqualToString:@"iPhone5,3"])    return @"iPhone 5C";
     if ([deviceString isEqualToString:@"iPhone5,4"])    return @"iPhone 5C";
     if ([deviceString isEqualToString:@"iPhone6,1"])    return @"iPhone 5S";
     if ([deviceString isEqualToString:@"iPhone6,2"])    return @"iPhone 5S";
     if ([deviceString isEqualToString:@"iPhone7,1"])    return @"iPhone 6 Plus";
     if ([deviceString isEqualToString:@"iPhone7,2"])    return @"iPhone 6";
     if ([deviceString isEqualToString:@"iPhone8,1"])    return @"iPhone 6s";
     if ([deviceString isEqualToString:@"iPhone8,2"])    return @"iPhone 6s Plus";
     if ([deviceString isEqualToString:@"iPhone8,4"])    return @"iPhone SE";
     if ([deviceString isEqualToString:@"iPhone9,1"])    return @"iPhone 7";
     if ([deviceString isEqualToString:@"iPhone9,3"])    return @"iPhone 7";
     if ([deviceString isEqualToString:@"iPhone9,2"])    return @"iPhone 7 Plus";
     if ([deviceString isEqualToString:@"iPhone9,4"])    return @"iPhone 7 Plus";
     if ([deviceString isEqualToString:@"iPhone10,1"])    return @"iPhone 8";
     if ([deviceString isEqualToString:@"iPhone10,4"])    return @"iPhone 8";
     if ([deviceString isEqualToString:@"iPhone10,2"])    return @"iPhone 8 Plus";
     if ([deviceString isEqualToString:@"iPhone10,5"])    return @"iPhone 8 Plus";
     if ([deviceString isEqualToString:@"iPhone10,3"])    return @"iPhone X";
     if ([deviceString isEqualToString:@"iPhone10,6"])    return @"iPhone X";
     if ([deviceString isEqualToString:@"iPhone11,8"])    return @"iPhone XR";
     if ([deviceString isEqualToString:@"iPhone11,2"])    return @"iPhone XS";
     if ([deviceString isEqualToString:@"iPhone11,6"])    return @"iPhone XS Max";
     if ([deviceString isEqualToString:@"iPhone12,1"])    return @"iPhone 11";
     if ([deviceString isEqualToString:@"iPhone12,3"])    return @"iPhone 11 Pro";
     if ([deviceString isEqualToString:@"iPhone12,5"])    return @"iPhone 11 ProMax";
     if ([deviceString isEqualToString:@"iPhone13,1"])    return @"iPhone 12";
     if ([deviceString isEqualToString:@"iPhone13,3"])    return @"iPhone 12 Pro";
    if ([deviceString isEqualToString:@"iPhone13,4"])    return @"iPhone 12 Pro Max";
    if ([deviceString isEqualToString:@"iPhone14,4"])    return @"iPhone 13 mini";
    if ([deviceString isEqualToString:@"iPhone14,5"])    return @"iPhone 13";
    if ([deviceString isEqualToString:@"iPhone14,2"])    return @"iPhone 13 Pro";
    if ([deviceString isEqualToString:@"iPhone14,3"])    return @"iPhone 13 Pro Max";
    if ([deviceString isEqualToString:@"iPhone14,6"])    return @"iPhone SE";
    if ([deviceString isEqualToString:@"iPhone14,7"])    return @"iPhone 14";
    if ([deviceString isEqualToString:@"iPhone14,8"])    return @"iPhone 14 Plus";
    if ([deviceString isEqualToString:@"iPhone15,2"])    return @"iPhone 14 Pro";
    if ([deviceString isEqualToString:@"iPhone15,3"])    return @"iPhone 14 Pro Max";


     //iPod
     if ([deviceString isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";
     if ([deviceString isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";
     if ([deviceString isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";
     if ([deviceString isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";
     if ([deviceString isEqualToString:@"iPod5,1"])      return @"iPod Touch 5G";
     
     //iPad
     if ([deviceString isEqualToString:@"iPad1,1"])      return @"iPad";
     if ([deviceString isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
     if ([deviceString isEqualToString:@"iPad2,2"])      return @"iPad 2 (GSM)";
     if ([deviceString isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
     if ([deviceString isEqualToString:@"iPad2,4"])      return @"iPad 2 (32nm)";
     if ([deviceString isEqualToString:@"iPad2,5"])      return @"iPad mini (WiFi)";
     if ([deviceString isEqualToString:@"iPad2,6"])      return @"iPad mini (GSM)";
     if ([deviceString isEqualToString:@"iPad2,7"])      return @"iPad mini (CDMA)";
     
     if ([deviceString isEqualToString:@"iPad3,1"])      return @"iPad 3(WiFi)";
     if ([deviceString isEqualToString:@"iPad3,2"])      return @"iPad 3(CDMA)";
     if ([deviceString isEqualToString:@"iPad3,3"])      return @"iPad 3(4G)";
     if ([deviceString isEqualToString:@"iPad3,4"])      return @"iPad 4 (WiFi)";
     if ([deviceString isEqualToString:@"iPad3,5"])      return @"iPad 4 (4G)";
     if ([deviceString isEqualToString:@"iPad3,6"])      return @"iPad 4 (CDMA)";
     
     if ([deviceString isEqualToString:@"iPad4,1"])      return @"iPad Air";
     if ([deviceString isEqualToString:@"iPad4,2"])      return @"iPad Air";
     if ([deviceString isEqualToString:@"iPad4,3"])      return @"iPad Air";
     if ([deviceString isEqualToString:@"iPad5,3"])      return @"iPad Air 2";
     if ([deviceString isEqualToString:@"iPad5,4"])      return @"iPad Air 2";
     if ([deviceString isEqualToString:@"i386"])         return @"Simulator";
     if ([deviceString isEqualToString:@"x86_64"])       return @"Simulator";
     
     if ([deviceString isEqualToString:@"iPad4,4"]
         ||[deviceString isEqualToString:@"iPad4,5"]
         ||[deviceString isEqualToString:@"iPad4,6"])      return @"iPad mini 2";
     
     if ([deviceString isEqualToString:@"iPad4,7"]
         ||[deviceString isEqualToString:@"iPad4,8"]
         ||[deviceString isEqualToString:@"iPad4,9"])      return @"iPad mini 3";
     
     return deviceString;

}

+ (NSString *)fileSizeWithInterge:(NSInteger)size {
      // 1k = 1024, 1m = 1024k
      if (size < 1024) { //小於1k
          return [NSString stringWithFormat:@"%ldB",(long)size];
      } else if (size < 1024 * 1024) { //小於1M
          CGFloat cFloat = size / 1024;
          return [NSString stringWithFormat:@"%.1fK",cFloat];
      } else if (size < 1024 * 1024 * 1024) { //小於1G
          CGFloat cFloat = size / (1024 * 1024);
          return [NSString stringWithFormat:@"%.1fM",cFloat];
      } else { //大於1G
          CGFloat cFloat = size / (1024 * 1024 * 1024);
          return [NSString stringWithFormat:@"%.1fG",cFloat];
      }
}

// 获取当前视图
+ (UIViewController *)currentViewController {
    UIViewController *resultVC;
    resultVC = [self topViewController:[self rootViewController]];
    while (resultVC.presentedViewController) {
        resultVC = [self topViewController:resultVC.presentedViewController];
    }
    return resultVC;
}

+ (NSString *)randomNonce:(NSInteger)length {
    NSAssert(length > 0, @"Expected nonce to have positive length");
    NSString *characterSet = @"0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._";
    NSMutableString *result = [NSMutableString string];
    NSInteger remainingLength = length;
    
    while (remainingLength > 0) {
        NSMutableArray *randoms = [NSMutableArray arrayWithCapacity:16];
        for (NSInteger i = 0; i < 16; i++) {
            uint8_t random = 0;
            int errorCode = SecRandomCopyBytes(kSecRandomDefault, 1, &random);
            NSAssert(errorCode == errSecSuccess, @"Unable to generate nonce: OSStatus %i", errorCode);
            [randoms addObject:@(random)];
        }
        
        for (NSNumber *random in randoms) {
            if (remainingLength == 0) {
                break;
            }
            if (random.unsignedIntValue < characterSet.length) {
                unichar character = [characterSet characterAtIndex:random.unsignedIntValue];
                [result appendFormat:@"%C", character];
                remainingLength--;
            }
        }
    }
    return result;
}

+ (NSString *)stringBySha256HashingString:(NSString *)input {
    const char *string = [input UTF8String];
    unsigned char result[CC_SHA256_DIGEST_LENGTH];
    CC_SHA256(string, (CC_LONG)strlen(string), result);
    
    NSMutableString *hashed = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH * 2];
    for (NSInteger i = 0; i < CC_SHA256_DIGEST_LENGTH; i++) {
        [hashed appendFormat:@"%02x", result[i]];
    }
    return hashed;
}

+ (NSString *)getVersionCode {
    NSString * version = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
    version = [version stringByReplacingOccurrencesOfString:@"." withString:@""];
    return SEEmptyStr(version);
}

+ (NSString *)fullWebUrl:(NSString *)url {
    NSMutableString *webUrl = [[NSMutableString alloc]initWithString:url];
    NSString * language = languageArray[appConfig.languageType];
    [webUrl appendFormat:@"?lang=%@",language];
    [webUrl appendFormat:@"&version_code=%@",[[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"]];
    [webUrl appendFormat:@"&name=%@",[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]];
    [webUrl appendFormat:@"&os_type=%@",@"ios"];
    [webUrl appendFormat:@"&channel=%@",@"AppStore"];
    [webUrl appendFormat:@"&user_id=%ld",(long)appUserCenter.id];
    if (!SEIsNullOrEmpty(appUserCenter.token)) {
        [webUrl appendFormat:@"&token=%@",appUserCenter.token];
    }
    return webUrl;
}
@end
